<!-- 入库管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="72px">
      <el-form-item label="耗材编号" prop="goods_id">
        <el-input v-model="queryParams.goods_id" placeholder="请输入耗材编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="耗材名称" prop="goods_name">
        <el-input v-model="queryParams.goods_name" placeholder="请输入耗材名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单类型" prop="type">
        <el-select clearable v-model="queryParams.type" placeholder="请选择">
          <el-option v-for="dict in order_typeList" :key="dict.value" :value="dict.value" :label="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select clearable v-model="queryParams.status" placeholder="请选择">
          <el-option v-for="dict in orderStatusList" :key="dict.value" :value="dict.value" :label="dict.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作人" prop="update_user_id">
        <InputAutocomplete
          v-model="iptVal"
          placeholder="请输入操作人"
          search_type="USER"
          @onSelect="(val) => (queryParams.update_user_id = val)"
        />
      </el-form-item>
      <el-form-item label="开始日期">
        <el-date-picker
          placeholder="请选择开始日期"
          v-model="queryParams.start_date"
          value-format="YYYY-MM-DD"
          type="date"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="结束日期">
        <el-date-picker
          placeholder="请选择结束日期"
          v-model="queryParams.end_date"
          value-format="YYYY-MM-DD"
          type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="user_type === 2">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">入库登记</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip :visible="tooltipShow">
          <template #content>
            <span>请先选择订单类型，订单状态，开始日期，结束日期，点搜索后才可打印</span>
          </template>
          <el-button
            type="primary"
            plain
            icon="Printer"
            @mouseenter="tooltipShow = canPrint"
            @mouseleave="tooltipShow = false"
            :disabled="canPrint"
            v-print="print"
          >
            打印
          </el-button>
        </el-tooltip>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="goodsList" id="table" border>
      <el-table-column label="耗材编号" prop="goods_id" />
      <el-table-column label="耗材名称" prop="goods_name" min-width="120" show-overflow-tooltip />
      <el-table-column label="耗材规格" prop="goods_spec" min-width="100" show-overflow-tooltip />
      <el-table-column label="订单类型" prop="type_name" width="90" />
      <el-table-column label="订单状态" prop="status_name" width="80" />
      <el-table-column label="入库数量" prop="qty" />
      <el-table-column label="取消数量" prop="cancel_qty" />
      <el-table-column label="补货日期" prop="order_date" width="160" />
      <el-table-column label="耗材批号" prop="batch_no" />
      <el-table-column label="耗材效期" prop="expiration_date" width="100" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="操作人" prop="update_user_name" width="120" show-overflow-tooltip />
      <el-table-column label="操作日期" prop="update_date" width="160" />
      <el-table-column label="操作" align="center" fixed="right" width="160" class-name="small-padding fixed-width">
        <template #default="scope">
          <template v-if="scope.row.status === 0">
            <el-button link type="primary" @click="handleCancel(scope.row)">取消入库</el-button>
          </template>
          <template v-if="user_type === 3 && scope.row.status === 0">
            <el-button link type="primary" @click="handleAudit(scope.row)">入库审核</el-button>
          </template>
          <template v-if="user_type === 3 && scope.row.status === 6">
            <el-button link type="primary" @click="handleReturn(scope.row)">退货</el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page_num"
      v-model:limit="queryParams.page_size"
      @pagination="getList"
    />

    <AddOrderDialog v-model="visible" @success="resetQuery" />
    <cancelDialog v-model="cancelVisible" :rowInfo="rowInfo" title="退货" @success="getList" />
    <PrintDialog :tableData="goodsList" :id="print.id" :type="queryParams.type" />
  </div>
</template>

<script setup name="Warehousing">
import AddOrderDialog from "./addOrderDialog.vue";
import cancelDialog from "./cancelDialog.vue";
import PrintDialog from "../components/printDialog.vue";
import { ElMessageBox } from "element-plus";
import useUserStore from "@/store/modules/user";

import { getOptionsList } from "@/api/common";
import { getInvRecordList, orderVerify, orderCancelVerify } from "@/api/inventory/warehousing";
import { computed, nextTick } from "vue";
const { proxy } = getCurrentInstance();
const store = useUserStore();
const user_type = computed(() => store.user_type);
const showSearch = ref(true);
const loading = ref(false);
const queryParams = ref({
  page_num: 1,
  page_size: 10,
  goods_name: "",
  goods_id: "",
  type: "",
  status: "",
  update_user_id: "",
  start_date: "",
  end_date: ""
});
const goodsList = ref([]);
const total = ref(0);
const order_typeList = ref([]);
const orderStatusList = ref([]);
const visible = ref(false);

const cancelVisible = ref(false);
const rowInfo = ref({});

const iptVal = ref("");

const searchBtnClicked = ref(false); // 点击搜索按钮标识
// 打印必须勾选查询条件: 订单类型、订单状态、开始日期、结束日期; 变更查询条件之后必须点击一次查询方可打印
const canPrint = computed(() => {
  const { type, status, start_date, end_date } = queryParams.value;
  return !(type && String(status) && start_date && end_date && searchBtnClicked.value) || !goodsList.value.length;
});
const tooltipShow = ref(false);

const print = {
  id: "warePrintTable",
  previewTitle: "入库管理列表",
  popTitle: " ", // 打印配置页上方的标题
  extraHead: "" // 最上方的头部文字，附加在head标签上的额外标签，使用逗号分割
};

watch(
  () => queryParams,
  () => {
    searchBtnClicked.value = false;
  },
  { deep: true }
);

getList();

getorder_typeList();
getOrderStatusList();

async function getorder_typeList() {
  const res = await getOptionsList({ dict_type: "ORDER_TYPE_IN" });
  if (res.code === 0) {
    order_typeList.value = res.data;
  }
}

async function getOrderStatusList() {
  const res = await getOptionsList({ dict_type: "ORDER_STATUS" });
  if (res.code === 0) {
    orderStatusList.value = res.data;
  }
}

/** 查询列表 */
function getList() {
  loading.value = true;
  getInvRecordList({ ...queryParams.value })
    .then((res) => {
      const data = res.data;
      goodsList.value = data.list;
      total.value = data.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_num = 1;
  nextTick(() => {
    searchBtnClicked.value = true;
  });

  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.update_user_id = "";
  queryParams.value.start_date = "";
  queryParams.value.end_date = "";
  iptVal.value = "";
  handleQuery();
}

// 入库登记
const handleAdd = () => {
  visible.value = true;
};
// 取消入库审核
const handleCancel = (row) => {
  ElMessageBox.prompt("请输入备注，可不填", "取消入库审核", {
    confirmButtonText: "确认",
    cancelButtonText: "取消"
  })
    .then(async ({ value }) => {
      const res = await orderCancelVerify({
        order_id: row.id,
        order_type: row.type,
        remark: value || ""
      });
      if (res && res.code === 0) {
        proxy.$modal.msgSuccess("取消成功");
        getList();
        return;
      }
      proxy.$modal.msgError(res.message);
    })
    .catch(() => {});
};
// 入库审核
const handleAudit = async (row) => {
  console.log(row);
  const res = await orderVerify({
    order_id: row.id,
    order_type: row.type
  });
  if (res && res.code === 0) {
    proxy.$modal.msgSuccess("入库审核成功");
    getList();
    return;
  }
  proxy.$modal.msgError(res.message);
};
// 退货
const handleReturn = async (row) => {
  rowInfo.value = row;
  cancelVisible.value = true;
};
</script>
